﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nature
{
    

    public partial class IdealGasMixture : IMixtureDefinition, ICloneable
    {
        public CHOFormula CHOFormula
        {
            get
            {
                return new CHOFormula(this);
            }
        }

        public ReadOnlyArray<string> SpeciesIDs 
        {
            [DebuggerStepThrough]
            get { return Model.SpeciesIDs; }
        }

        public ReadOnlyArray<string> ElementIDs
        {
            [DebuggerStepThrough]
            get { return Model.ElementIDs; }
        }

        public ReadOnlyArray<double> SpeciesMolarMasses
        {
            [DebuggerStepThrough]
            get { return Model.SpeciesMolarMasses; }
        }

        
        public ReadOnlyArray<double> ElementMolarMasses
        {
            [DebuggerStepThrough]
            get { return Model.ElementMolarMasses; }
        }

        public int GetSpeciesIndex(string speciesID)
        {
            return Model.GetSpeciesIndex(speciesID);
        }


        public string ToString(double xmin)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" [T] = {0}", Temperature);
            sb.AppendFormat("\r\n [P] = {0}", Pressure / Constants.Atmosphere);
            for(int isp = 0; isp < Nsp; ++isp)
            {
                double x = SpeciesMoleFractions[isp];
                if (x >= xmin)
                {
                    sb.AppendFormat("\r\n {0} = {1}", SpeciesIDs[isp], x);
                }
            }
            return sb.ToString();
        }

        public override string ToString()
        {
            return this.ToString(1.0e-5);
        }

        public IdealGasMixture Clone()
        {
            return this.Model.CreateIdealGasMixture(this);
        }

        #region IMixtureDefinition Members

        ReadOnlyArray<double> IMixtureDefinition.X
        {
            get { return this.SpeciesMoleFractions.ToArray(); }
        }

        ReadOnlyArray<string> IMixtureDefinition.SpeciesIDs
        {
            get { return Model.SpeciesIDs; }
        }

        Temperature IMixtureDefinition.Temperature
        {
            get { return this.Temperature; }
        }

        Pressure IMixtureDefinition.Pressure
        {
            get { return this.Pressure; }
        }

        #endregion

        #region ICloneable Members

        object ICloneable.Clone()
        {
            return this.Clone();
        }

        #endregion
    }

    public partial struct ReadOnlyIdealGasMixture
    {
        public ModelObject Model
        {
            get { return m_mixture.Model; }
        }
    }
}
